/** SysAdmin.java */

package edu.gatech.userclasses;

import edu.gatech.datastorage.DoctorsOrders;
import edu.gatech.utilities.Persistence;


/**
 * System administrator has same priveledges as any other user,
 * plus the abilty to edit users
 * (ie. add/delete users, susupend and unsuspend users from loggin in).
 * @author Dan Holdridge
 *
 * @version $Revision: 1.0 $
 */
public class SysAdmin extends Doctor{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6963571720240276209L;

	/**
	 * Standard constructor
	 * @param name String
	 */
	public SysAdmin(String name){
		this.name = name;
		this.userType = "System Administrator";
	}
	
	/**
	 * Constructor for registering
	 * @param username
	 * @param password
	 */
	public SysAdmin(String username, String password){
		this.username = username;
		this.password = password;
		this.name = username;
		this.userType = "System Administrator";
	}

	/**Adds a user to the database.
	 * This is the only way to add Nurses, Doctors, or
	 * SysAdmins. Nurses and Doctors can add Patients.

	 * @param type The new user's type (Patient, Nurse, etc).
	 * @param username String
	 * @param password String
	
	 * @return True if a user was added * @see Nurse.addPatient */
	public boolean addUser(String username, String password, UserType type){ // $codepro.audit.disable booleanMethodNamingConvention
		
		if (username == null || password == null){
			System.out.println("Error, no user added");
			return false;
		}
		User newUser = null;
		switch (type){
		case PATIENT:
			newUser = new Patient(username, password);
			Persistence.getUserList().add((Patient) newUser);
			break;
		case NURSE:
			newUser = new Nurse(username, password);
			Persistence.getUserList().add((Nurse) newUser);
			break;
		case DOCTOR:
			newUser = new Doctor(username, password);
			Persistence.getUserList().add((Doctor) newUser);
			break;
		case SYSADMIN:
			newUser = new SysAdmin(username, password);
			Persistence.getUserList().add((SysAdmin) newUser);
			break;
		default:
			System.out.println("Error, no user added.");
			return false;
		}
		Persistence.refresh();
		return true;
		
	}
	
	/** 
	 * Searches for a specific user
	 * @param username The name of the user to search for
	
	 * @return The user if found, else null */
	public User searchUsers(String username){
		User temp = null;
		for (User u : Persistence.getUserList()){
			if (u.getUsername().equals(username)){
				temp = u;
			}
		}
		return temp;
	}
	
	/**
	 * Removes a user from the database
	 * @param u User
	 */
	public void deleteUser(User u){
		Persistence.getUserList().remove(u);
		Persistence.refresh();
	}
	
	/**
	 * Suspends a user, preventing them from logging in.
	 * @param u The user being suspended
	 */
	public void suspend(User u){
		u.setSuspended(true);
	}
	
	/**
	 * Removes suspension from a user, whether they were suspended
	 * by an admin or got locked out after too many failed logins.
	 * @param u The user whose suspension is being removed.
	 */
	public void unsuspend(User u){
		u.setSuspended(false);
	}

	/**
	 * Overrides the <code>addOrders</code> method from the Doctor
	 * class, preventing the SysAdmin from adding Doctors Orders to a 
	 * Patient's TreatmentRecord. (Don't need admins prescribing drugs, 
	 * do we?)
	 * 
	 * 
	 * 
	 * @param patient Patient
	 * @param doc DoctorsOrders
	 * @param followUp boolean
	 */
	@Override
	public void addOrders(Patient patient, DoctorsOrders doc, boolean followUp) {
		System.out.println("Sorry, you are not authorized to do that./n" +
				"Only Doctors can add orders.");
	}
}
